package com.tiyende.credit.loop.event

/**
 * EventPublisher
 * @Author wwdeng
 * @CreateTime: 2024/10/16 10:51
 * @Annotation：发布者
 */
class TiyendeEventPublisher {

    private var subscriberList: ArrayList<TiyendeEventSubscriber>? = null
    init {
        subscriberList = arrayListOf()
    }

    /**
     * 注册订阅者
     */
    fun register(subscriber: TiyendeEventSubscriber){
        var isAdd = true
        subscriberList?.let {
            for (i in 0..< it.size){
                if (it[i] == subscriber){
                    isAdd = false
                    break
                }
            }
        }
        if (isAdd){
            subscriberList?.add(subscriber)
        }
    }

    /**
     * 发布信息
     */
    fun publish(event: TiyendeEventData){
        subscriberList?.forEach {
            it.onTiyendePublishEvent(event)
        }
    }

    /**
     * 移除订阅者
     */
    fun detach(subscriber: TiyendeEventSubscriber){
        subscriberList?.remove(subscriber)
    }
}